package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Base.Connect;
import com.bjqh.edu.Bean.Grade;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Fragment.OneDayFragemnt;
import com.bjqh.edu.UI.Fragment.TwoDayFragment;
import com.bjqh.edu.adapter.Send2Adapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;


public class CoursePurchaseActivity extends FragmentActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    private Send2Adapter adapter;
    List<Map<String, String>> list = new ArrayList<Map<String,String>>();
   private List<Grade.RowsBean> gradeBean=new ArrayList<>();
    private  Grade grade;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_course_purchase);
        ButterKnife.bind(this);
        initData();
        tvTitle.setText("课程购买");


        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    protected void initViews() {

    }

    protected void initData() {
        OkGo.post(AppConstants.BASE_URL + "/admin/omMenu//datalist.shtml?")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("flag", "0")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        getValue(s);

                    }
                });

    }

    protected void initEvents() {

    }
    public void getValue(String s) {
        Gson gson = new Gson();
        Log.e("购买课程列表",s+"");
        Grade grade = gson.fromJson(s, Grade.class);
        gradeBean = grade.getRows();

        if(gradeBean!=null&&gradeBean.size()>0)
        {


          //  Toast.makeText(CoursePurchaseActivity.this, "请求成功"+gradeBean.get(0).getName(), Toast.LENGTH_SHORT).show();
            for(int i=0;i<gradeBean.size();i++){
                tlTab.addTab(tlTab.newTab().setText(gradeBean.get(i).getName()));
            }
            List<Fragment> fragments = new LinkedList<Fragment>();

            Connect connect = new Connect();
            for(int i=0;i<gradeBean.size();i++){
                connect.setActivity(CoursePurchaseActivity.this);
                if (gradeBean.get(i).getFlag()==0)
                {
                    fragments.add(OneDayFragemnt.newInstance(connect,gradeBean.get(i).getId()));
                }
                else {
                    fragments.add(TwoDayFragment.newInstance(connect,gradeBean.get(i).getId()));

                }

            }
            tvTitle.setTag(R.id.tv_title,gradeBean.get(0).getId());
            tlTab.getTabAt(0).select();
            adapter = new Send2Adapter(getSupportFragmentManager(), fragments,gradeBean);//初始化fragment类型adapter,datas为adapter构造函数参数
            vpContent.setAdapter(adapter);
            //加入title选择和被选择的颜色,前面为未点击的颜色,后面为点击后的颜色
            tlTab.setTabTextColors(ContextCompat.getColor(this, R.color.black_text), ContextCompat.getColor(this, R.color.pink));
            tlTab.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.pink));
            tlTab.setupWithViewPager(vpContent);//将tablayout与viewpager建立关系
            tlTab.setTabsFromPagerAdapter(adapter);

            tlTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });





        }

    }
}
